Completed
Pull Request — master (#141)
by Maxence
02:40
created

_.extend.updateEmptyContent   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
nc 3
nop 0
1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
(function() {
26
	/**
27
	 * @class OCA.Circles.FileList
28
	 * @augments OCA.Files.FileList
29
	 *
30
	 * @classdesc Circles file list.
31
	 * Contains a list of files filtered by circles
32
	 *
33
	 * @param $el container element with existing markup for the #controls
34
	 * and a table
35
	 * @param [options] map of options, see other parameters
36
	 * @param {Array.<string>} [options.circlesIds] array of system tag ids to
37
	 * filter by
38
	 */
39
	var FileList = function($el, options) {
40
		this.initialize($el, options);
41
	};
42
	FileList.prototype = _.extend({}, OCA.Files.FileList.prototype,
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable OCA seems to be never declared. If this is a global, consider adding a /** global: OCA */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
43
		/** @lends OCA.Circles.FileList.prototype */ {
44
		id: 'circlesfilter',
45
		appName: t('circles', 'Circles\' files'),
46
47
		/**
48
		 * Array of system tag ids to filter by
49
		 *
50
		 * @type Array.<string>
51
		 */
52
		_circlesIds: [],
53
		_lastUsedTags: [],
54
55
		_clientSideSort: true,
56
		_allowSelection: false,
57
58
		_filterField: null,
59
60
		/**
61
		 * @private
62
		 */
63
		initialize: function($el, options) {
64
			OCA.Files.FileList.prototype.initialize.apply(this, arguments);
0 ignored issues
show
Bug introduced by
The variable OCA seems to be never declared. If this is a global, consider adding a /** global: OCA */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
65
			if (this.initialized) {
66
				return;
67
			}
68
69
			if (options && options.circlesIds) {
70
				this._circlesIds = options.circlesIds;
71
			}
72
73
			OC.Plugins.attach('OCA.Circles.FileList', this);
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
74
75
			var $controls = this.$el.find('#controls').empty();
76
77
			this._initFilterField($controls);
78
		},
79
80
		destroy: function() {
81
			this.$filterField.remove();
82
83
			OCA.Files.FileList.prototype.destroy.apply(this, arguments);
0 ignored issues
show
Bug introduced by
The variable OCA seems to be never declared. If this is a global, consider adding a /** global: OCA */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
84
		},
85
86
		_initFilterField: function($container) {
87
			var self = this;
88
			this.$filterField = $('<input type="hidden" name="circles"/>');
89
			$container.append(this.$filterField);
90
			this.$filterField.select2({
91
				placeholder: t('circles', 'Select circles to filter by'),
92
				allowClear: false,
93
				multiple: true,
94
				toggleSelect: true,
95
				separator: ',',
96
				query: _.bind(this._queryCirclesAutocomplete, this),
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
97
98
				id: function(circle) {
99
					return circle.unique_id;
100
				},
101
102
				initSelection: function(element, callback) {
103
					var val = $(element).val().trim();
104
					if (val) {
105
						var circleIds = val.split(','),
106
							circles = [];
107
108
						OCA.Circles.api.listCircles("all", '', 1, function(result) {
0 ignored issues
show
Bug introduced by
The variable OCA seems to be never declared. If this is a global, consider adding a /** global: OCA */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
109
							_.each(circleIds, function(circleId) {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
110
								var circle = _.find(result.data,function(circleData) {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
111
									return circleData.unique_id == circleId;
112
								});
113
								if (!_.isUndefined(circle)) {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
114
									circles.push(circle);
115
								}
116
							});
117
118
							callback(circles);
119
						});
120
121
					} else {
122
						callback([]);
123
					}
124
				},
125
126
				formatResult: function (circle) {
127
					return circle.name;
128
				},
129
130
				formatSelection: function (circle) {
131
					return circle.name;
132
					//return OC.SystemTags.getDescriptiveTag(tag)[0].outerHTML;
133
				},
134
135
				sortResults: function(results) {
136
					results.sort(function(a, b) {
137
						var aLastUsed = self._lastUsedTags.indexOf(a.id);
138
						var bLastUsed = self._lastUsedTags.indexOf(b.id);
139
140
						if (aLastUsed !== bLastUsed) {
141
							if (bLastUsed === -1) {
142
								return -1;
143
							}
144
							if (aLastUsed === -1) {
145
								return 1;
146
							}
147
							return aLastUsed < bLastUsed ? -1 : 1;
148
						}
149
150
						// Both not found
151
						return OC.Util.naturalSortCompare(a.name, b.name);
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
152
					});
153
					return results;
154
				},
155
156
				escapeMarkup: function(m) {
157
					// prevent double markup escape
158
					return m;
159
				},
160
				formatNoMatches: function() {
161
					return t('systemtags', 'No circles found');
162
				}
163
			});
164
			this.$filterField.on('change', _.bind(this._onTagsChanged, this));
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
165
			return this.$filterField;
166
		},
167
168
		/**
169
		 * Autocomplete function for dropdown results
170
		 *
171
		 * @param {Object} query select2 query object
172
		 */
173
		_queryCirclesAutocomplete: function(query) {
174
175
			OCA.Circles.api.listCircles("all", query.term, 1, function(result) {
0 ignored issues
show
Bug introduced by
The variable OCA seems to be never declared. If this is a global, consider adding a /** global: OCA */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
176
				query.callback({
177
					results: result.data
178
				});
179
			});
180
			/*
181
			 OC.SystemTags.collection.fetch({
182
			 success: function() {
183
			 var results = OC.SystemTags.collection.filterByName(query.term);
184
185
			 query.callback({
186
			 results: _.invoke(results, 'toJSON')
187
			 });
188
			 }
189
			 });
190
			 */
191
		},
192
193
		/**
194
		 * Event handler for when the URL changed
195
		 */
196
		_onUrlChanged: function(e) {
197
			if (e.dir) {
198
				var circles = _.filter(e.dir.split('/'), function(val) { return val.trim() !== ''; });
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
199
				this.$filterField.select2('val', circles || []);
200
				this._circlesIds = circles;
201
				this.reload();
202
			}
203
		},
204
205
		_onTagsChanged: function(ev) {
206
			var val = $(ev.target).val().trim();
207
			if (val !== '') {
208
				this._circlesIds = val.split(',');
209
			} else {
210
				this._circlesIds = [];
211
			}
212
213
			this.$el.trigger(jQuery.Event('changeDirectory', {
214
				dir: this._circlesIds.join('/')
215
			}));
216
			this.reload();
217
		},
218
219
		updateEmptyContent: function() {
220
			var dir = this.getCurrentDirectory();
221
			if (dir === '/') {
222
				// root has special permissions
223
				if (!this._circlesIds.length) {
224
					// no tags selected
225
					this.$el.find('#emptycontent').html('<div class="icon-systemtags"></div>' +
226
						'<h2>' + t('systemtags', 'Please select circles to filter by') + '</h2>');
227
				} else {
228
					// tags selected but no results
229
					this.$el.find('#emptycontent').html('<div class="icon-systemtags"></div>' +
230
						'<h2>' + t('systemtags', 'No files found for the selected circles') + '</h2>');
231
				}
232
				this.$el.find('#emptycontent').toggleClass('hidden', !this.isEmpty);
233
				this.$el.find('#filestable thead th').toggleClass('hidden', this.isEmpty);
234
			}
235
			else {
236
				OCA.Files.FileList.prototype.updateEmptyContent.apply(this, arguments);
0 ignored issues
show
Bug introduced by
The variable OCA seems to be never declared. If this is a global, consider adding a /** global: OCA */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
237
			}
238
		},
239
240
		getDirectoryPermissions: function() {
241
			return OC.PERMISSION_READ | OC.PERMISSION_DELETE;
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
242
		},
243
244
		updateStorageStatistics: function() {
245
			// no op because it doesn't have
246
			// storage info like free space / used space
247
		},
248
249
		reload: function() {
250
			if (!this._circlesIds.length) {
251
				// don't reload
252
				this.updateEmptyContent();
253
				this.setFiles([]);
254
				return $.Deferred().resolve();
255
			}
256
257
			this._selectedFiles = {};
258
			this._selectionSummary.clear();
259
			if (this._currentFileModel) {
260
				this._currentFileModel.off();
261
			}
262
			this._currentFileModel = null;
263
			this.$el.find('.select-all').prop('checked', false);
264
			this.showMask();
265
			this._reloadCall = this.filesClient.getFilteredFiles(
266
				{
267
					circlesIds: this._circlesIds
268
				},
269
				{
270
					properties: this._getWebdavProperties()
271
				}
272
			);
273
			if (this._detailsView) {
274
				// close sidebar
275
				this._updateDetailsView(null);
276
			}
277
			var callBack = this.reloadCallback.bind(this);
278
			return this._reloadCall.then(callBack, callBack);
279
		},
280
281
		reloadCallback: function(status, result) {
282
			if (result) {
283
				// prepend empty dir info because original handler
284
				result.unshift({});
285
			}
286
287
			return OCA.Files.FileList.prototype.reloadCallback.call(this, status, result);
0 ignored issues
show
Bug introduced by
The variable OCA seems to be never declared. If this is a global, consider adding a /** global: OCA */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
288
		}
289
	});
290
291
	OCA.Circles.FileList = FileList;
292
})();
293
294